package com.qian.saas.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qian.saas.domain.Renwu;
import com.qian.saas.domain.Rep;
import com.qian.saas.domain.Users;
import com.qian.saas.domain.Vo.RenwuVo;
import com.qian.saas.mapper.RenwuMapper;
import com.qian.saas.mapper.UsersMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/renwu")
public class RenwuController {

    @Resource
    private RenwuMapper renwuMapper;

    @Resource
    private UsersMapper usersMapper;

    @GetMapping("/list")
    public R list(String page, String limit) {
        IPage<Renwu> mppage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));//参数一：当前页，参数二：每页记录数
        //分页
        IPage<Renwu> userIPage = renwuMapper.selectPage(mppage, null);
        List<Renwu> records = userIPage.getRecords();
        List<RenwuVo> renwuVos = new ArrayList();
        for (Renwu record : records) {
            RenwuVo renwuVo = new RenwuVo();
            renwuVo.setRenwuid(record.getRenwuid());
            renwuVo.setUserid(record.getUserid());
            renwuVo.setCreatetime(record.getCreatetime());
            renwuVo.setStarttime(record.getStarttime());
            renwuVo.setEndtime(record.getEndtime());
            renwuVo.setInter(record.getInter());
            renwuVo.setQt(record.getQt());
            Integer userid = record.getUserid();
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("id", userid);
            Users users = usersMapper.selectOne(wrapper);
            renwuVo.setUsername(users.getUsername());
            renwuVos.add(renwuVo);
        }
        Map map = new HashMap<>();
        map.put("items", renwuVos);
        map.put("total", userIPage.getTotal());
        return R.success(map);
    }

    @PostMapping("/renwuupdate")
    public R add(@RequestBody Renwu renwu){
        System.out.println(renwu);
        renwu.setCreatetime(LocalDateTime.now());
        renwuMapper.insert(renwu);
        return R.success("sdfg");
    }

    @GetMapping("renwudel")
    public R del(int renwuid){
        System.out.println("-------");
        System.out.println(renwuid);
        QueryWrapper<Renwu> wrapper=new QueryWrapper();
        wrapper.eq("renwuid",renwuid);
        renwuMapper.delete(wrapper);
        return R.success("sdfg");
    }
}
